package com.cn.ccp.action.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.MenuBean;

/**
 * 用户访问权限action
 * @author Administrator
 *
 */
public class MenuAction extends BaseAction {

	private MenuBean menuBean ;
	private List<MenuBean> menuList ;
	private SelectItem[] menuSelectItem ;
	
	public MenuAction(){
		getpagedetail();
	}
	
	public String edit(){
		boolean flag = getMenuService().menuNameIsAlready(menuBean.getId() ,menuBean.getMenuName());
		if(flag){
			showMsg("rolename",  "menu_page_label_name_isalready");
			return "";
		}
		if(menuBean.getId() == 0)
			return save();
		return update();
	}
	
	public String save(){
		boolean flag = getMenuService().sava(menuBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = getMenuService().update(menuBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public void delete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		getMenuService().delete(Integer.parseInt(id));
		getpagedetail();
	}
	
	public void showBean(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		if(id !=null)
			menuBean = (MenuBean) getMenuService().findById(Integer.parseInt(id));
	}
	
	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		menuBean = (MenuBean) getMenuService().findById(Integer.parseInt(id));
//		getAllOfID(Integer.parseInt(id));
		return UPDATE;
	}
	public String goAdd(){
		getAllOfID(0);
		return ADD;
	}
	
	
	
	@Override
	public void getpagedetail() {
		setItemCount(getMenuService().getCount());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		menuList = getMenuService().pageQuery(start, pagesize);
	}
	
	public void getAllOfID(int id){
//		menuList = getMenuService().getAllOfID(id);
//		if(menuList != null && menuList.size() > 0){
//			menuSelectItem = new SelectItem[menuList.size() + 1];
//			menuSelectItem[0] = new SelectItem("0","父目录");
//			for (int i = 0; i < menuList.size(); i++) {
//				MenuBean bean = menuList.get(i);
//				menuSelectItem[i+1] = new SelectItem(String.valueOf(bean.getId()).trim(),bean.getMenuName());
//			}
//		}
	}
	

	public MenuBean getMenuBean() {
		if(menuBean == null)
			menuBean = new MenuBean();
		return menuBean;
	}

	public void setMenuBean(MenuBean menuBean) {
		this.menuBean = menuBean;
	}

	public List<MenuBean> getMenuList() {
		if(menuList == null)
			menuList = new ArrayList<MenuBean>();
		return menuList;
	}

	public void setMenuList(List<MenuBean> menuList) {
		this.menuList = menuList;
	}

	public SelectItem[] getMenuSelectItem() {
		if(menuSelectItem == null){
			if(menuBean == null){
				menuSelectItem = new SelectItem[1];
				menuSelectItem[0] = new SelectItem("0","父目录");
			}else{
				menuList = getMenuService().pageQuery();
				if(menuList != null && menuList.size() > 0){
					menuSelectItem = new SelectItem[menuList.size() + 1];
					menuSelectItem[0] = new SelectItem("0","父目录");
					for (int i = 0; i < menuList.size(); i++) {
						MenuBean bean = menuList.get(i);
						menuSelectItem[i+1] = new SelectItem(String.valueOf(bean.getId()).trim(),bean.getMenuName());
					}
				}else{
					menuSelectItem = new SelectItem[1];
					menuSelectItem[0] = new SelectItem("0","父目录");
				}
			}
		}
		return menuSelectItem;
	}

	public void setMenuSelectItem(SelectItem[] menuSelectItem) {
		this.menuSelectItem = menuSelectItem;
	}

}
